
console.log("===========================script head =================================")

console.log("Frida.version:" + Frida.version);

console.log("Frida.heapSize:" + "0x" + Frida.heapSize.toString(16));

console.log("Script.runtime:" + Script.runtime);

console.log('current PID:' + Process.id);

function log(msg) {
    console.log(msg);
}

function Java_fullStackTrace() {
    var result = [];
    var jstackTraces = Java.use("java.lang.Thread").currentThread().getStackTrace();
    for (var i = 0; i < jstackTraces.length; i++) {
        var item = jstackTraces[i];
        result.push(item.toString() + "\n");
    }
    return result;
}

function Java_printStackTrace(start = 2, depth = 12) {
    console.log('***********************|start|****************************')
    var stackTraces = Java_fullStackTrace();
    var max = stackTraces.length;
    console.log(stackTraces.slice(start, start + depth));
    console.log('***********************| end |****************************\n')
}




function batchHookJavaClass(className) {
    var jclass = Java.use(className)
    var jmethodArray = jclass.class.getDeclaredMethods()

    for (var i = 0; i < jmethodArray.length; i++) {
        var jmethod = jmethodArray[i]
        var methodName = jmethod.getName()
        var count = jclass[methodName].overloads.length
        for (var index = 0; index < count; index++) {
            jclass[methodName].overloads[index].implementation = function () {
                // number of parameters
                // var n_arg_cnt = arguments.length;
                /*
                for (var idx_arg = 0; idx_arg < n_arg_cnt; n_arg_cnt++)
                {
                    console.log(arguments[idx_arg]);
                }
                */
                log('call ' + className + "." + methodName)
                Java_printStackTrace()
                return this[methodName].apply(this, arguments)
            }
        }
    }
}

function main() {
    log("=-=-=-=-=-=-=");

    Java.use('java.lang.Runtime').exec.overload('[Ljava.lang.String;', '[Ljava.lang.String;', 'java.io.File').implementation = function (cmdarray, envp, dir) {
        log('call Runtime.exec ' + cmdarray + ', ' + envp + ', ' + dir)
        if (String(cmdarray[1]).indexOf('ro.cls') > -1) {
            Java_printStackTrace()
        }
        return this.exec(cmdarray, envp, dir);
    }


    Java.use('android.os.Build').getString.overload('java.lang.String').implementation = function (key) {
        log('call Build.getString (' + key + ' )')
        return this.getString(key)
    }


    var SystemProperties = Java.use('android.os.SystemProperties')
    SystemProperties.get.overload('java.lang.String').implementation = function (key) {
        log('call SystemProperties.get (' + key + ' )')
        return SystemProperties.get(key)
    }

    SystemProperties.get.overload('java.lang.String', 'java.lang.String').implementation = function (key, def) {
        log('call SystemProperties.get (' + key + ', ' + def + ' )')
        return SystemProperties.get(key, def)
    }

    SystemProperties.getInt.overload('java.lang.String', 'int').implementation = function (key, def) {
        log('call SystemProperties.getInt (' + key + ', ' + def + ' )')
        return SystemProperties.getInt(key, def)
    }

    SystemProperties.getLong.overload('java.lang.String', 'long').implementation = function (key, def) {
        log('call SystemProperties.getLong (' + key + ', ' + def + ' )')
        return SystemProperties.getLong(key, def)
    }

    SystemProperties.getBoolean.overload('java.lang.String', 'boolean').implementation = function (key, def) {
        log('call SystemProperties.getBoolean( ' + key + ', ' + def + ' )')
        return SystemProperties.getBoolean(key, def)
    }


    var androidProviderSettingSystem = Java.use('android.provider.Settings$System')
    androidProviderSettingSystem.getStringForUser.overload('android.content.ContentResolver', 'java.lang.String', 'int').implementation = function (resolver, name, userHandle) {
        log('call SettingSystem.getStringForUser (' + name + ' )')
        if (name == "android_id") {
            Java_printStackTrace();
        }
        return androidProviderSettingSystem.getStringForUser(resolver, name, userHandle)
    }


    var androidProviderSettingSecure = Java.use('android.provider.Settings$Secure')
    androidProviderSettingSecure.getStringForUser.overload('android.content.ContentResolver', 'java.lang.String', 'int').implementation = function (resolver, name, userHandle) {
        log('call SettingSecure.getStringForUser (' + name + ' )')
        if (name == "android_id") {
            Java_printStackTrace();
        }
        return androidProviderSettingSecure.getStringForUser(resolver, name, userHandle)
    }


    var androidProviderSettingGlobal = Java.use('android.provider.Settings$Global')
    androidProviderSettingGlobal.getStringForUser.overload('android.content.ContentResolver', 'java.lang.String', 'int').implementation = function (resolver, name, userHandle) {
        log('call SettingGlobal.getStringForUser (' + name + ' )')
        if (name == "android_id") {
            Java_printStackTrace();
        }
        return androidProviderSettingGlobal.getStringForUser(resolver, name, userHandle)
    }



    Java.use('android.content.pm.PackageManager').getInstalledPackages.overload('int').implementation = function (flag) {
        log('call PackageManager.getInstalledPackages')
        Java_printStackTrace();
        return this.getInstalledPackages(flag)
    }



    Java.use('java.net.InetAddress').getHostAddress.implementation = function () {
        log('call InetAddress.getHostAddress')
        Java_printStackTrace();
        return this.getHostAddress()
    }



    Java.use('java.net.NetworkInterface').getHardwareAddress.implementation = function () {
        log('call NetworkInterface.NetworkInterface')
        Java_printStackTrace();
        return this.getHardwareAddress()
    }


    Java.use('android.net.wifi.WifiInfo').getMacAddress.implementation = function () {
        log('call WifiInfo.getMacAddress')
        Java_printStackTrace();
        return this.getMacAddress()
    }

    // batchHookJavaClass('android.telephony.TelephonyManager')

    Java.use('android.telephony.TelephonyManager').getSubscriberId.overload().implementation = function () {
        log('call TelephonyManager.getSubscriberId()')
        Java_printStackTrace()
        var result = this.getSubscriberId()
        return result;
    }


    Java.use('android.telephony.TelephonyManager').getSubscriberId.overload('int').implementation = function (subId) {
        log('call TelephonyManager.getSubscriberId(' + subId  +')')
        Java_printStackTrace()
        var result = this.getSubscriberId(subId)
        return result;
    }

    Java.use('android.telephony.TelephonyManager').getDeviceId.overload().implementation = function () {
        log('call TelephonyManager.getDeviceId()')
        Java_printStackTrace()
        var result = this.getDeviceId()
        return result;
    }


    Java.use('android.telephony.TelephonyManager').getDeviceId.overload('int').implementation = function (slotIndex) {
        log('call TelephonyManager.getDeviceId(' + slotIndex + ')')
        Java_printStackTrace()
        var result = this.getDeviceId(slotIndex)
        return result;
    }



    Java.use('android.telephony.TelephonyManager').getImei.overload().implementation = function () {
        log('call TelephonyManager.getImei()')
        Java_printStackTrace()
        var result = this.getImei()
        return result;
    }


    Java.use('android.telephony.TelephonyManager').getImei.overload('int').implementation = function (slotIndex) {
        log('call TelephonyManager.getImei(' + slotIndex + ')')
        Java_printStackTrace()
        var result = this.getImei(slotIndex)
        return result;
    }



    Java.use('android.telephony.TelephonyManager').getMeid.overload().implementation = function () {
        log('call TelephonyManager.getMeid()')
        Java_printStackTrace()
        var result = this.getMeid()
        return result;
    }



    Java.use('android.telephony.TelephonyManager').getMeid.overload('int').implementation = function (slotIndex) {
        log('call TelephonyManager.getMeid(' + slotIndex + ')')
        Java_printStackTrace()
        var result = this.getMeid(slotIndex)
        return result;
    }


    Java.use('android.location.LocationManager').getLastKnownLocation.overload('java.lang.String').implementation = function (provider) {
        log('call TelephonyManager.getLastKnownLocation(String)')
        Java_printStackTrace()
        var result = this.getLastKnownLocation(provider)
        return result;
    }


    // Java.use('android.location.LocationManager').requestLocationUpdates.overload('android.location.LocationRequest', 'java.util.concurrent.Executor', 'android.location.LocationListener').implementation = function (locationRequest, executor, listener) {
    //     log('call TelephonyManager.requestLocationUpdates(String)')
    //     Java_printStackTrace()
    //     var result = this.requestLocationUpdates(locationRequest, executor, listener)
    //     return result;
    // }


    Java.use('android.location.LocationManager').requestLocationUpdates.overload('android.location.LocationRequest', 'android.app.PendingIntent').implementation = function (locationRequest, pendingIntent) {
        log('call TelephonyManager.requestLocationUpdates(String)')
        Java_printStackTrace()
        var result = this.requestLocationUpdates(locationRequest, pendingIntent)
        return result;
    }


    // Java.use('android.location.LocationManager').getCurrentLocation.overload('android.location.LocationRequest', 'android.os.CancellationSignal', 'java.util.concurrent.Executor', 'java.util.function.Consumer').implementation = function (locationRequest, cancellationSignal,executor,consumer ) {
    //     log('call TelephonyManager.requestLocationUpdates(String)')
    //     Java_printStackTrace()
    //     var result = this.getCurrentLocation(locationRequest, cancellationSignal,executor,consumer)
    //     return result;
    // }


    Java.use('android.app.ActivityManager').getRunningAppProcesses.overload().implementation = function(){
        log('call android.app.ActivityManager.getRunningAppProcesses()')
        Java_printStackTrace()
        return this.getRunningAppProcesses()
    }


    Java.use('android.app.ApplicationPackageManager').getInstalledApplications.overload('int').implementation = function(flags){
        log('call android.app.ApplicationPackageManager.getInstalledApplications(' + flags + ')')
        Java_printStackTrace()
        return this.getInstalledApplications(flags)
    }

    Java.use('android.app.ApplicationPackageManager').getInstalledPackages.overload('int').implementation = function(flags){
        log('call android.app.ApplicationPackageManager.getInstalledPackages(' + flags + ')')
        Java_printStackTrace()
        return this.getInstalledApplications(flags)
    }
    // java.net.Inet4Address
    Java.use('java.net.Inet4Address').toString.overload().implementation = function( ){
        log('call java.net.Inet4Address  toString()')
        Java_printStackTrace()
        return this.toString()
    }
    Java.use('java.net.Socket').connect.overload('java.net.SocketAddress').implementation = function(p1 ){
        log('call java.net.Socket  connect()')
        Java_printStackTrace()
        return this.connect(p1)
    }
    // (SocketAddress endpoint, int timeout)
    Java.use('java.net.Socket').connect.overload('java.net.SocketAddress','int').implementation = function(p1,p2 ){
        log('call java.net.Socket  connect()')
        Java_printStackTrace()
        return this.connect(p1,p2)
    }
    // at java.net.Socket.connect(Socket.java:621)
    // Java.use('java.lang.String').toString.overload().implementation = function( ){
    //     log('call java.lang.String  toString()')
    //     Java_printStackTrace()
    //     return this.toString()
    // }
}

Java.perform(main);


console.log("===========================script  end =================================")

//frida -U -f com.gnsbt.Shgame.nearme.gamecenter --no-pause -l ghpilgin.js
//frida -U -f com.qy.sgzc.aligames --no-pause -l ghpilgin.js
//frida -U -f com.shiyue.sszg.mz --no-pause -l ghpilgin.js
//frida -U -f org.cocos2dx.cpp_tests --no-pause -l ghpilgin.js
//frida -U -f org.cocos2dx.cpp_tests --no-pause -l ghpilgin.js
//frida -U -f com.xlxx.sshg.aligames --no-pause -l ghpilgin.js
//frida -U -f com.lilithgames.afk.aligames --no-pause -l ghpilgin.js
//frida -U -f com.zhanhuo.hxshz.aligames --no-pause -l ghpilgin.js
//frida -U -f com.droidhang.ad2.vivo --no-pause -l ghpilgin.js
//frida -U -f com.gh.gamecenter --no-pause -l ghpilgin.js
//frida -U -f com.fxj.zhijian.mi --no-pause -l ghpilgin.js
//frida -U com.droidhang.ad2.vivo -l ghpilgin.js